import ArrayUtil from '@/ArrayUtil';
import AttributeUtil from '@/AttributeUtil';
import ForceUtil from '@/ForceUtil';
import PlayerUtil from '@/PlayerUtil';
import RandomUtil from '@/RandomUtil';
import SyncUtil from '@/SyncUtil';

import { data as common_heros, type data_type as ChooseHeroConfigModel } from 'xlsx/common_heros';

interface ChooseHeroDataModel {
    refreshCount: number;
    /**
     * 可以选择的英雄数据列表
     */
    hadHeroList: ChooseHeroConfigModel[];
    /**
     * 筛选过的英雄数据列表，可以从中3选1来创建英雄
     */
    filterHeroList: ChooseHeroConfigModel[];
}

export default class UIChooseHeroModel {
    spawnPos: { x: number; y: number }[] = [
        {
            x: -1400,
            y: 2400,
        },
        {
            x: 0,
            y: 1000,
        },
        {
            x: 2400,
            y: 2400,
        },
        {
            x: -1400,
            y: 2100,
        },
        {
            x: 0,
            y: -1350,
        },
        {
            x: 2000,
            y: -1800,
        },
    ];
    constructor() {
        // 初始化选择英雄需要的数据
        this.initPlayersResource();

        // 监听刷新按钮点击事件
        SyncUtil.onSyncData('hero_refresh_button_click', this.refreshButtonClickHandler.bind(this));

        // 监听选择英雄按钮点击事件
        SyncUtil.onSyncObjData('choose_hero_button_click', this.chooseButtonClickHandler.bind(this));
    }

    // 点击按钮创建英雄
    chooseButtonClickHandler(p: player, data: ChooseHeroConfigModel) {
        let talent = TalentManager.getTalentById(data.TalentId);
        let pos = ArrayUtil.randomElement(this.spawnPos);
        let u = CreateUnit(p, data.id, pos.x, pos.y, 0);

        let min = 30 - game_hard_level * 3;
        let max = 80 - game_hard_level * 8;
        AttributeUtil.addUnitAttribute(u, {
            自定义敏捷: data.CusAGI,
            自定义智力: data.CusINT,
            基础闪避: -0.05,
            法术暴击机率: -0.05,
            物理暴击机率: -0.05,
            // 根据难度 随机奖励 攻击、法强、护甲、魔抗
            attack: RandomUtil.nextInt(min, max),
            法术强度: RandomUtil.nextInt(min, max),
            附加护甲: RandomUtil.nextInt(min, max),
            附加魔抗: RandomUtil.nextInt(min, max),
        });
        talent.addTalent(u);
        // 通知其它模块
        se.emit('created_hero', { hero: u, player: p });
    }

    // 刷新按钮点击更新数据
    refreshButtonClickHandler(p: player) {
        let id = GetPlayerId(p);
        let db = this.DB[id];
        db.refreshCount--;
        db.filterHeroList = this.randomHeros(id);
        se.emit('choose_hero_data_update', p);
    }
    DB: ChooseHeroDataModel[] = [];
    initPlayersResource() {
        let count = ForceUtil.getUserCount();
        let rate = 50 + 50 * count;
        PlayerUtil.message(`当前在线玩家数量：|cffdd3300${count}|r,经验获得率为：|cffffcc00${rate}%`);

        for (let i = 0; i < count; i++) {
            SetPlayerHandicapXPBJ(Player(i), rate);
            this.DB[i] = {
                refreshCount: 150,
                hadHeroList: ArrayUtil.shuffle(common_heros),
                filterHeroList: [],
            };
        }
    }
    randomPlayerHeros(p: player) {
        let id = GetPlayerId(p);
        return this.randomHeros(id);
    }
    randomHeros(id: number) {
        let temp = [...this.DB[id].hadHeroList];
        let old = this.DB[id].filterHeroList;
        // 先移除掉已经选过的英雄
        for (let h of temp) {
            if (old.includes(h)) {
                ArrayUtil.removeElement(temp, h);
            }
        }
        if (temp.length < 3) {
            throw new Error('玩家英雄数量不足');
        }

        temp = ArrayUtil.shuffle(temp);
        let tars: ChooseHeroConfigModel[] = [];
        while (tars.length < 3) {
            let tar = ArrayUtil.randomElement(temp);
            if (tars.includes(tar)) continue;
            tars.push(tar);
        }
        return tars;
    }
    getPlayerRemainRefresh(p: player) {
        return this.DB[GetPlayerId(p)].refreshCount;
    }
    getPlayerFilterHeros(p: player) {
        return this.DB[GetPlayerId(p)].filterHeroList;
    }
}
